#include <vector>
#include <unordered_set>
using namespace std;
class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        // 创建哈希表去重
        unordered_set<int> myset;
        for (auto& x : nums)
        {
            myset.insert(x);
        }

        int maxlength = 0;
        for (auto& num : myset)
        {
            if (!myset.count(num- 1)) //理解为什么num-1是重点
            {
                int currnum = num, length = 1;
                while (myset.count(++currnum))
                {
                    length++;
                }
                if (maxlength < length)
                    maxlength = length;
            }
        }
        return maxlength;
    }
};





